local skynet = require "skynet"
local filelog = require "filelog"

require "hbslenum"

local server = nil
local tablerequest = {}


function tablerequest.init(service)
	if server == nil then server = service end
end

function tablerequest.entertable(rid, roledata, msgbody)
	local table_data = server.table_data
	local seat = nil
	local tablelogic = server:get_logicbyname("tablelogic")
	local responsemsg = {}
	----进桌不判断携带量
	----要关服了,不让玩家进入
	if table_data.conf.is_closed == true then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "敬请期待"
		skynet.pcall(skynet.retpack, responsemsg)
		return
	end
	if table_data.table_end_flag == true then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "服务器维护中"
		skynet.pcall(skynet.retpack, responsemsg)
		return 
	end

	if table_data.sitdown_player_num >= table_data.conf.max_player_num then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "房间人数已满"
		skynet.pcall(skynet.retpack, responsemsg)
		return 
	end

	seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat then
		-- responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		-- responsemsg.retstr = "已经在桌内"
		responsemsg.table_gameinfo = {
			tableconf = {},
			tablebaseinfo = {},
			burylandmineinfo = {},
			seatroleinfos = {},
			seatgameinfos = {},
			selfroleinfo = {},
			selfgameinfo = {},
			todoinfo = {},
			can_gethbsinfo = {},
			hb_num_lists  = {},
			hb_money_lists = {},
		}
		tablelogic.entertable(table_data, rid, msgbody, roledata, seat, true)
		tablelogic.copy_tablegameinfo(responsemsg.table_gameinfo, rid)
		skynet.pcall(skynet.retpack, responsemsg)
		return 
	end
	seat = tablelogic.get_emptyseat_by_index(table_data)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "没有空位"
		skynet.pcall(skynet.retpack, responsemsg)
		return 
	end
	tablelogic.entertable(table_data, rid, msgbody, roledata, seat)
	responsemsg.table_gameinfo = {
		tableconf = {},
		tablebaseinfo = {},
		burylandmineinfo = {},
		seatroleinfos = {},
		seatgameinfos = {},
		selfroleinfo = {},
		selfgameinfo = {},
		todoinfo = {},
		can_gethbsinfo	= {},	
		hb_num_lists = {}, 		
		hb_money_lists = {},
	}
	tablelogic.copy_tablegameinfo(responsemsg.table_gameinfo, rid)
	skynet.pcall(skynet.retpack, responsemsg)
end
--[[
	message doaction {
		optional int32 action_type 		= 1; //操作类型 (1抢红包 2申请埋雷)
		optional int32 hb_total_money 	= 2; // 红包金额
		optional int32 landmine_num 	= 3; // 雷号
		optional int32 bury_hb_num 		= 4; // 一次操作埋雷个数(无限模式使用)
		optional int32 hb_nums          = 5; // 一个雷中有几个包(无限模式使用，可以是7包和10包)
		optional string hb_id 			= 6; // 红包id(无限模式使用)
	}

	message doactionR {
		optional int32 				action_type 		= 1; //操作类型 (1抢红包 2申请埋雷)
		optional int32 				is_success_qhb 		= 2; //是否抢成功
		optional int32 				qhb_get_money 		= 3; //抢到的金额
		optional int32				is_landmine 		= 4; // 是否中雷 
		repeated lastplayergethb 	last_playergethbs 	= 5; //最近10局抢到红包金额(无限模式使用) 
	}
--]]


function tablerequest.doaction(rid, roledata, msgbody)
	local responsemsg = {}
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	local seatlogic = server:get_logicbyname("seatlogic")
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "没在桌内"
		skynet.pcall(skynet.retpack, responsemsg)
		return
	end
	if msgbody.action_type == HBSLActionType.ACTION_TYPE_SQML then
		if msgbody.hb_total_money <= 0 then
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = "发红包金额错误"
			skynet.pcall(skynet.retpack, responsemsg)
			return
		end
		if msgbody.hb_total_money > seat.diamond then
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = "余额不足,不能埋雷"
			skynet.pcall(skynet.retpack, responsemsg)
			return 
		end
		if msgbody.hb_total_money < table_data.conf.min_hb_amount or msgbody.hb_total_money > table_data.conf.max_hb_amount then
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = "红包金额超出场次范围"
			skynet.pcall(skynet.retpack, responsemsg)
			return
		end
		if msgbody.hb_total_money % 1000 ~= 0 then
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = "红包金额只能是10的整数"
			skynet.pcall(skynet.retpack, responsemsg)
			return
		end
		if not msgbody.landmine_num  or (msgbody.landmine_num < 0 or msgbody.landmine_num > 9) then
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = "雷号不合法"
			skynet.pcall(skynet.retpack, responsemsg)
			return 
		end
		if table_data.conf.play_game_type == HBSLPlayGameType.GAME_TYPE_COMMON then
			--[[
			local has_buryinfo = tablelogic.get_bury_info_byrid(rid)
			if has_buryinfo then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "已经申请不能重复申请"
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
			--]]
			local want_num = msgbody.bury_hb_num
			local one_hb_money = msgbody.hb_total_money
			local want_hb_nums = msgbody.hb_nums
			if not want_num or not one_hb_money or (want_hb_nums ~= table_data.conf.hb_nums) then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "红包数量不对"
				skynet.pcall(skynet.retpack, responsemsg)
				return 
			end
			local need_all_money = one_hb_money * want_num
			if need_all_money > seat.diamond then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "埋雷总额超过余额,不能埋雷"
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
			local has_buryinfos = tablelogic.get_bury_infos_byrid(rid)
			if #has_buryinfos + want_num > HBSLMAXWantNum then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "队列中你的红包数量太多"
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
		elseif table_data.conf.play_game_type == HBSLPlayGameType.GMAE_TYPE_UNLIMIT then
			---判断身上的钱够不够埋多个雷
			local want_num = msgbody.bury_hb_num
			local one_hb_money = msgbody.hb_total_money
			local want_hb_nums = msgbody.hb_nums
			if not want_num or not one_hb_money or (want_hb_nums ~= HBSLBuryHBNums.HB_NUM_SEVEN and want_hb_nums ~= HBSLBuryHBNums.HB_NUM_TEN) then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "无限模式红包数量不对"
				skynet.pcall(skynet.retpack, responsemsg)
				return 
			end
			local need_all_money = one_hb_money * want_num
			if need_all_money > seat.diamond then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "埋雷总额超过余额,不能埋雷"
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
			local has_buryinfos = tablelogic.get_bury_infos_byrid(rid)
			if #has_buryinfos + want_num > HBSLMAXWantNum then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "队列中你的红包数量太多"
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
		end
		tablelogic.apply_bury_landmine(rid, seat, msgbody)
		responsemsg.action_type = msgbody.action_type
		skynet.pcall(skynet.retpack, responsemsg)
	elseif msgbody.action_type == HBSLActionType.ACTION_TYPE_QHB then
		if table_data.state == HBSLTableState.TABLE_STATE_WAIT_PLAYER_QHB then
			if table_data.conf.play_game_type == HBSLPlayGameType.GAME_TYPE_COMMON then
				if next(table_data.cur_landmineinfo) == nil 
				or table_data.check_hbinfo_timer_id ~= -1 then
					responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
					responsemsg.retstr = "当前没红包可抢"
					skynet.pcall(skynet.retpack, responsemsg)
					return
				end
				---判断抢红包进额
				if seat.diamond < (table_data.cur_landmineinfo.total_money * table_data.conf.times) then
					responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
					responsemsg.retstr = "余额不足,不能抢红包!"
					skynet.pcall(skynet.retpack, responsemsg)
					return 
				end
				---检查红包数量
				local left_hb_num = tablelogic.get_left_hb_num()
				if left_hb_num <= 0 then
					responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
					responsemsg.retstr = "红包抢完了"
					skynet.pcall(skynet.retpack, responsemsg)
					return
				end
				----检查是否抢了红包,如果抢了就不能再抢了
				local has_qiang_hb = tablelogic.check_player_already_qiang(rid)
				if has_qiang_hb == true then
					responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
					responsemsg.retstr = "已经抢过红包了"
					skynet.pcall(skynet.retpack, responsemsg)
					return
				end
				if roledata.info.level == 99 then
					if roledata.money.diamond ~= seat.diamond then
						seat.diamond = roledata.money.diamond
						seat.playerinfo.diamond = roledata.money.diamond 
					end
				end
				local is_success, get_money, is_landmine = tablelogic.qhb_deal(rid, seat, msgbody)
				---filelog.sys_error("------is_success, get_money------", rid, is_success, get_money)
				---发回抢红包的结果
				responsemsg.action_type = msgbody.action_type
				responsemsg.is_success_qhb = 1
				responsemsg.qhb_get_money = get_money
				responsemsg.is_landmine = is_landmine
				responsemsg.last_playergethbs = {}
				skynet.pcall(skynet.retpack, responsemsg)
				tablelogic.notice_already_qhb_infos()
				tablelogic.check_hb_is_qw()
				return
			else
				responsemsg.action_type = msgbody.action_type
				responsemsg.is_success_qhb = 2
				responsemsg.qhb_get_money = 0
				responsemsg.is_landmine = 2
				responsemsg.last_playergethbs = {}
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
		elseif table_data.conf.play_game_type == HBSLPlayGameType.GMAE_TYPE_UNLIMIT then
			local want_qhb_hb_id = msgbody.hb_id
			local get_cur_hb_info = tablelogic.get_can_qianghb_nolimit(want_qhb_hb_id)
			if not get_cur_hb_info then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "这个id的红包不存在！"
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
			---判断抢红包金额
			if seat.diamond < get_cur_hb_info.total_money * get_cur_hb_info.times then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "余额不足,不能抢红包!"
				skynet.pcall(skynet.retpack, responsemsg)
				return 
			end
			---检查红包数量
			if get_cur_hb_info.already_qhb_num == get_cur_hb_info.total_hb_num or get_cur_hb_info.is_over == true then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "红包抢完了"
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
			---检查是否抢了这个红包
			local has_qiang_hb = tablelogic.check_is_already_qhbnolimit(rid, want_qhb_hb_id)
			if has_qiang_hb == true then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "已经抢过红包了"
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
			local is_success, get_money, is_landmine = tablelogic.qhb_deal_nolimit(rid, seat, msgbody)
			if is_success == false then
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "没有抢到！！！"
				skynet.pcall(skynet.retpack, responsemsg)
				return
			end
			responsemsg.action_type = msgbody.action_type
			responsemsg.is_success_qhb = 1
			responsemsg.qhb_get_money = get_money
			responsemsg.is_landmine = is_landmine
			responsemsg.last_playergethbs = {}
			seatlogic.copy_last_playergethbs(seat, responsemsg.last_playergethbs)
			skynet.pcall(skynet.retpack, responsemsg)
			return
		end
	elseif msgbody.action_type == HBSLActionType.ACTION_TYPE_CANCELHB then
		if seat.playerinfo.level ~= 99 then
			responsemsg.action_type = msgbody.action_type
			skynet.pcall(skynet.retpack, responsemsg)
			return
		end
		local has_buryinfo = tablelogic.get_bury_info_byrid(rid)
		if not has_buryinfo then
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = ""
			skynet.pcall(skynet.retpack, responsemsg)
			return
		else
			tablelogic.cancel_bury(rid)
			responsemsg.action_type = msgbody.action_type
			skynet.pcall(skynet.retpack, responsemsg)
			return
		end
	end
end

function tablerequest.getapplylist(rid, roledata, msgbody)
	local responsemsg = {
		applyburyinfos = {}
	}
	local tablelogic = server:get_logicbyname("tablelogic")
	tablelogic.copy_apply_buryinfo(responsemsg.applyburyinfos)
	skynet.pcall(skynet.retpack, responsemsg)
end

function tablerequest.getplayerlist(rid, roledata, msgbody)
	local responsemsg = {
		seatroleinfos = {},
		seatgameinfos = {},
	}
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	local players_temp = {}
	for index, seat in ipairs(table_data.seats) do
		if seat.rid > 0 then
			local player_item = { index = index, rid = seat.rid, diamond = seat.diamond }
			table.insert(players_temp, player_item)
		end
	end
	table.sort(players_temp, function(first, second) 
			if first.diamond > second.diamond then 
				return true
			else
				return false
			end
		end)
	-- local from = 1
	-- local endto = #players_temp
	-- if not msgbody.player_num or msgbody.player_num == 0 or msgbody.player_num == 1 then
	-- 	if #players_temp <= 50 then
	-- 		from = 1
	-- 		endto = #players_temp
	-- 	else
	-- 		from = 1
	-- 		endto = 50
	-- 	end
	-- else
	-- 	if  endto >= (msgbody.player_num - 1) * 50 then
	-- 		from = (msgbody.player_num - 1) * 50
	-- 		if msgbody.player_num * 50 > endto then

	-- 		else
	-- 			endto = msgbody.player_num * 50
	-- 		end
	-- 	else
	-- 		from = 0
	-- 		endto = 0
	-- 	end
	-- end
	-- --filelog.sys_error("-------tablerequest.getplayerlist--rid------------", rid, msgbody.player_num, from, endto)
	-- if endto - from > 50 then endto = from + 50 end
	-- --filelog.sys_error("-------tablerequest.getplayerlist--from, endto----------", rid, msgbody.player_num, from, endto)
	-- local len_infos = 0
	---for i = from, endto do
	for index, playertemp in ipairs(players_temp) do
		local seat = tablelogic.get_seat_by_rid(table_data, playertemp.rid)
		if seat then
			local base_seatroleinfo = {}
			tablelogic.copy_roleinfo(base_seatroleinfo, seat, true)
			local base_seatgameinfo = {}
			tablelogic.copy_gameinfo(base_seatgameinfo, seat, true)
			table.insert(responsemsg.seatroleinfos, base_seatroleinfo)
			---table.insert(responsemsg.seatgameinfos, base_seatgameinfo)
			---len_infos = len_infos + 1
		end
	end
	----filelog.sys_error("-----------responsemsg-----------", responsemsg)
	skynet.pcall(skynet.retpack, responsemsg)
end

function tablerequest.leavetable(rid, roledata, msgbody)
	---filelog.sys_error("-------tablerequest.leavetable--------", rid)
	local table_data = server.table_data
	local responsemsg = {}
	local tablelogic = server:get_logicbyname("tablelogic")
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		server:notice_tableinfo_togatenode("role_leave_table", rid, skynet.getenv("nodename"), 0)
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "没在桌内"
		skynet.pcall(skynet.retpack, responsemsg)
		return
	else
		---看是否在埋雷申请列表中
		local bury_info, bury_index = tablelogic.get_bury_info_byrid(rid)
		if bury_info and bury_index then
			table.remove(table_data.wantbury_landmineinfo, bury_index)
		end
		---看是否在发红包
		local bury_landmineplayer = table_data.cur_landmineinfo
		if next(bury_landmineplayer) ~= nil then
			if bury_landmineplayer.rid == seat.rid then
				---seat.next_leave = true
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "抢红包不能退出"
				skynet.pcall(skynet.retpack, responsemsg)
				return 
			end
		end
		local qhb_info = tablelogic.get_qhb_info(seat.rid)
		if qhb_info then
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = "抢红包结算后退出"
			skynet.pcall(skynet.retpack, responsemsg)
			return 
		end
		tablelogic.leavetable(rid, seat)
		server:notice_tableinfo_togatenode("role_leave_table", rid, skynet.getenv("nodename"), 0)
		skynet.pcall(skynet.retpack, responsemsg)
	end
end

function tablerequest.disconnect(rid, roledata, msgbody)
	local responsemsg = {}
	local tablelogic = server:get_logicbyname("tablelogic")
	local table_data = server.table_data
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat then
		local bury_info, bury_index = tablelogic.get_bury_info_byrid(rid)
		if bury_info and bury_index then
			table.remove(table_data.wantbury_landmineinfo, bury_index)
		end
		local bury_landmineplayer = table_data.cur_landmineinfo
		if next(bury_landmineplayer) ~= nil then
			if bury_landmineplayer.rid == seat.rid then
				seat.next_leave = true
				responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
				responsemsg.retstr = "抢红包不能退出"
				skynet.pcall(skynet.retpack, responsemsg)
				return 
			end
		end
		local qhb_info = tablelogic.get_qhb_info(seat.rid)
		if qhb_info then
			seat.next_leave = true
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = "抢红包结算后退出"
			skynet.pcall(skynet.retpack, responsemsg)
			return 
		end
		tablelogic.leavetable(rid, seat)
		server:notice_tableinfo_togatenode("role_leave_table", rid, skynet.getenv("nodename"), 0)
	end
	skynet.pcall(skynet.retpack, responsemsg)
end

function tablerequest.delay_leave(rid, roledata, msgbody)
	local responsemsg = {}
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat then
		seat.next_leave = true
	end	
	skynet.pcall(skynet.retpack, responsemsg)
end

function tablerequest.notice_table_robotopen()
	local responsemsg = {}
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	local tablerobothelper = server:get_logicbyname("tablerobothelper")
    tablerobothelper.check_robot_numer()
end

function tablerequest.reentertable(rid, roledata, msgbody)
	local responsemsg = {}
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	local responsemsg = {}
	----进桌不判断携带量
	----要关服了,不让玩家进入
	if table_data.table_end_flag == true then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "服务器维护中"
		skynet.pcall(skynet.retpack, responsemsg)
		return 
	end
	seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat then
		responsemsg.game_info = {
			tableconf = {},
			tablebaseinfo = {},
			burylandmineinfo = {},
			seatroleinfos = {},
			seatgameinfos = {},
			selfroleinfo = {},
			selfgameinfo = {},
			todoinfo = {},
			can_gethbsinfo = {},
			hb_num_lists = {},
			hb_money_lists 	= {},
		}
		responsemsg.is_bury = 1
		local bury_info, bury_index = tablelogic.get_bury_info_byrid(rid)
		if bury_info and bury_index then
			responsemsg.is_bury = 2
		end
		---看是否在发红包
		-- local bury_landmineplayer = table_data.cur_landmineinfo
		-- if next(bury_landmineplayer) ~= nil and bury_landmineplayer.rid == seat.rid then
		-- 	responsemsg.is_bury = 2
		-- end
		tablelogic.copy_tablegameinfo(responsemsg.game_info, rid)
		skynet.pcall(skynet.retpack, responsemsg)
		return
	else
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "没在桌内"
		server:notice_tableinfo_togatenode("role_leave_table", rid, skynet.getenv("nodename"), table_data.conf.table_index)
		---tablelogic.leavetable(rid, seat)
		skynet.pcall(skynet.retpack, responsemsg)
	end
end


function tablerequest.getpubtablelist(rid, roledata, msgbody)
	local responsemsg = {}
	local tablelogic = server:get_logicbyname("tablelogic")
	local table_data = server.table_data
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat then
		tablelogic.leavetable(rid, seat)
	end
	server:notice_tableinfo_togatenode("role_leave_table", rid, skynet.getenv("nodename"), 0)
	skynet.pcall(skynet.retpack, responsemsg)
end

return tablerequest